/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package collections;

/**
 *
 * @author Iskandar Sarkawi
 */
public class ArrayExample {

    public static void main(String[] args) {

        demoStringArray();

        //ArrayExample arr = new ArrayExample();
        //arr.demoIntArray();//this is not a static class so must create an instance
    }

    public static void demoStringArray(){
        String[] notes;              // declares an array of integers

        notes = new String[3];      // allocates memory for 3 integers
        //notes = new String[]; //error. You must declare the size of the array

        notes[0] = "mynotes1"; // initialize first element
        notes[1] = "mynotes2"; // initialize second element
        notes[2] = "mynotes3"; // etc.

        System.out.println("Element at index 0: " + notes[0]);
        System.out.println("Element at index 1: " + notes[1]);
        System.out.println("Element at index 2: " + notes[2]);

        //notes[3] = "mynotes3"; // error, must manually increase the size of array        
        /*
         * Arrays cannot be resized dynamically. 
         If you want a dynamic data structure with random access, you use a Collection (Map, ArrayList,...).
         If you need to expand, you can use System.arraycopy() method to copy the content of an array to another one.
         */

        /*
         * Although copying an array isn't particularly difficult,
         it is an operation which benefits from a native implementation.
         Therefore java.lang.System includes a static System.arraycopy() method you can use to
         copy one array to another.
                public static void arraycopy(Object source, int sourcePosition,
                Object destination, int destinationPosition, int numberOfElements)
         */
        String[] temp;
        temp = new String[5];
        System.arraycopy(notes, 0, temp, 0, 3);
        notes = new String[5];
        System.arraycopy(temp, 0, notes, 0, 5);

        notes[3] = "mynotes4";
        notes[4] = "mynotes5";


        System.out.println("\nResized Array");
        System.out.println("Element at index 0: " + notes[0]);
        System.out.println("Element at index 1: " + notes[1]);
        System.out.println("Element at index 2: " + notes[2]);
        System.out.println("Element at index 3: " + notes[3]);
        System.out.println("Element at index 4: " + notes[4]);
    }
    
    public void demoIntArray(){
        int[] anArray;              // declares an array of integers

        anArray = new int[3];      // allocates memory for 10 integers

        anArray[0] = 100; // initialize first element
        anArray[1] = 200; // initialize second element
        anArray[2] = 300; // etc.

        System.out.println("Element at index 0: " + anArray[0]);
        System.out.println("Element at index 1: " + anArray[1]);
        System.out.println("Element at index 2: " + anArray[2]);
    }

}
